package com.sinosoft.dsp.platform.components.portal.input.elements;

import java.util.Date;

import org.hibernate.Query;

import com.sinosoft.builder.templates.concept.TagElementParameterType;
import com.sinosoft.builder.templates.concept.TagElementType;
import com.sinosoft.dsp.platform.components.helper.TagElementHelper;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.utils.DateUtils;

public class DateRangerTagElememt implements ISinoRangeTagElement {

	private static final Log log = Log.getLog(DateRangerTagElememt.class
			.getName());

	public static final String PARAMETER_PATTERN = "pattern";

	public static final String PARAMETER_MIN = "min";

	public static final String PARAMETER_MAX = "max";

	public static final String PARAMETER_INCLUDE_LOWER = "includeLower";

	public static final String PARAMETER_INCLUDE_UPPER = "includeUpper";

	private Date min;

	private Date max;

	private boolean includeLower = true;

	private boolean includeUpper = true;

	public Date getMin() {
		return min;
	}

	public void setMin(Date min) {
		this.min = min;
	}

	public Date getMax() {
		return max;
	}

	public void setMax(Date max) {
		this.max = max;
	}

	@Override
	public void setParameter(Query query, String key) {
		if (hasLower()) {
			query.setDate(key + "_" + PARAMETER_MIN, min);
		}
		if (hasUpper()) {
			query.setDate(key + "_" + PARAMETER_MAX, max);
		}
	}

	@Override
	public boolean isEmpty() {
		if (this.min == null && this.max == null) {
			return true;
		}
		return false;
	}

	@Override
	public boolean hasLower() {
		return !(this.min == null);
	}

	@Override
	public boolean hasUpper() {
		return !(this.max == null);
	}

	@Override
	public boolean isIncludeLower() {
		return includeLower;
	}

	@Override
	public void setIncludeLower(boolean includeLower) {
		this.includeLower = includeLower;
	}

	@Override
	public boolean isIncludeUpper() {
		return includeUpper;
	}

	@Override
	public void setIncludeUpper(boolean includeUpper) {
		this.includeUpper = includeUpper;
	}

	@Override
	public void initValue(TagElementType type) throws SinoPlatformException {
		try {
			if (type != null) {
				TagElementParameterType param = TagElementHelper
						.getTagElementParameter(type, PARAMETER_PATTERN);
				TagElementParameterType minParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_MIN);
				TagElementParameterType maxParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_MAX);
				TagElementParameterType includeLowerParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_INCLUDE_LOWER);
				TagElementParameterType includeUpperParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_INCLUDE_UPPER);
				if (!TagElementHelper.isEmptyTagElementParameter(param)) {
					if (!TagElementHelper.isEmptyTagElementParameter(minParam)) {
						this.min = DateUtils.str2dat(minParam.getValue(), param
								.getValue());
					}
					if (!TagElementHelper.isEmptyTagElementParameter(maxParam)) {
						this.max = DateUtils.str2dat(maxParam.getValue(), param
								.getValue());
					}
				} else {
					if (!TagElementHelper.isEmptyTagElementParameter(minParam)) {
						this.min = DateUtils.str2dat(minParam.getValue());
					}
					if (!TagElementHelper.isEmptyTagElementParameter(maxParam)) {
						this.max = DateUtils.str2dat(maxParam.getValue());
					}
				}

				if (!TagElementHelper
						.isEmptyTagElementParameter(includeLowerParam)) {
					this.includeLower = Boolean.valueOf(includeLowerParam
							.getValue());
				}
				if (!TagElementHelper
						.isEmptyTagElementParameter(includeUpperParam)) {
					this.includeUpper = Boolean.valueOf(includeUpperParam
							.getValue());
				}
			}
		} catch (Exception e) {
			log.error("初始化日期范围值出现错误。");
			throw new SinoPlatformException("初始化日期范围值出现错误。");
		}
	}
}
